package com.qf.service.impl;

import com.qf.config.RedisKey;
import com.qf.mapper.TeacherMapper;
import com.qf.pojo.respVo.BaseResp;
import com.qf.pojo.vo.entity.QfTeach;
import com.qf.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 陌人不故
 * @Date: 2022/10/27/1:17
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Resource
    TeacherMapper teacherMapper;
    @Autowired
    RedisTemplate redisTemplate;
    
    @Override
    public BaseResp findAll() {
        List range1 = redisTemplate.boundListOps(RedisKey.TEACHER_LIST).range(0, -1);
        if (range1 == null || range1.size() == 0){
            List<QfTeach> qfTeaches = teacherMapper.selectList(null);
            if (qfTeaches == null || qfTeaches.size() == 0){
                redisTemplate.opsForList().rightPush(RedisKey.TEACHER_LIST,"null");
                redisTemplate.expire(RedisKey.TEACHER_LIST,30, TimeUnit.SECONDS);
            }
            redisTemplate.opsForList().rightPushAll(RedisKey.TEACHER_LIST,qfTeaches);
        }
        List range = redisTemplate.boundListOps(RedisKey.TEACHER_LIST).range(0, -1);
        return BaseResp.SUCCESS("查询成功",range,null);
    }

    @Override
    public BaseResp findById(Integer id) {
        Boolean aBoolean = redisTemplate.boundHashOps(RedisKey.TEACHER_LIST_INFO).hasKey(id.toString());
        if (!aBoolean){
            QfTeach qfTeach = teacherMapper.selectById(id);
            redisTemplate.boundHashOps(RedisKey.TEACHER_LIST_INFO).put(id.toString(),qfTeach);
        }
        Object o = redisTemplate.boundHashOps(RedisKey.TEACHER_LIST_INFO).get(id.toString());
        return BaseResp.SUCCESS("教师查询成功",o,null);
    }
}
